@extends('public.pub')
@section('title','编辑用户')
@section('content')
    <div class="row">
        <ol class="breadcrumb">
            <li><a href="#">后台首页</a></li>
            <li class="active">用户修改</li>
        </ol>
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <form class="form-horizontal" role="form">
                <div class="form-group">
                    <legend>修改用户权限</legend>
                </div>
                <div class="form-group">
                    <label for="username" class="col-sm-2 control-label">用户名：</label>
                    <div class="col-sm-5">
                        <span class="form-control">{{ $data->username }}</span>
                    </div>
                </div>
                <div class="form-group">
                    <label for="role_id" class="col-sm-2 control-label">权限：</label>
                    <div class="col-sm-5">
                        <select name="role_id" id="role_id" class="form-control">
                            <option value="0"> -- 请选择 --</option>
                            @foreach($roles as $role)
                                <option @if(isset($data->role->id) && $role->id === $data->role->id)
                                        selected
                                        @endif
                                        value="{{ $role->id }}">{{ $role->name }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <button type="submit" class="btn btn-primary">修改</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
@endsection
@section('js')
    <script src="/admin/lib/layer/2.4/layer.js"></script>
    <script>
		$('.btn-primary').click(function () {
			let _token = '{{ csrf_token() }}';
			let role_id = $('#role_id').val();
			$.ajax({
				url: '{{ route('admin.user.edit',['user'=>$data->id]) }}',
				type: 'put',
				data: {_token, role_id},
				success(res) {
					if (res.status == 0) {
						layer.alert('修改成功')
					} else {
						layer.alert('修改失败')
					}
				}
			});
			return false;
		})
    </script>
@endsection